package Mundo;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Fernando
 */
import java.util.*;
import java.io.*;
import java.awt.image.*;

public class Mapa implements Serializable{
    private final int m;
    private final int n;
    private final String nombre;
    public Celda[][] celdas;

    public String getNombre() {
        return nombre;
    }

    
    public int getM() {
        return m;
    }

    public int getN() {
        return n;
    }
    
    public Mapa(String nombre,String nombreArchMapa,ArrayList<BufferedImage> tiles) throws FileNotFoundException {
        this.nombre=nombre;
        File map= new File(nombreArchMapa);
        Scanner sc = new Scanner(map);
        this.m = sc.nextInt();
        this.n = sc.nextInt();
        sc.nextLine();
        this.celdas = new Celda[this.n][this.m];
        for(int i=0;i<this.n;i++){
            String fila=sc.nextLine();
            for (int j=0;j<this.m;j++){
                BufferedImage img =null;
                if (fila.charAt(j)==',')img=tiles.get(6);
                if (fila.charAt(j)=='M')img=tiles.get(5);
                if (fila.charAt(j)=='L')img=tiles.get(4);
                if (fila.charAt(j)=='E')img=tiles.get(3);
                if (fila.charAt(j)=='e')img=tiles.get(2);
                if (fila.charAt(j)=='.')img=tiles.get(1);
                if (fila.charAt(j)==' ')img=tiles.get(0);
                this.celdas[i][j]= new Celda(fila.charAt(j),i,j,img);
            }
        }
    }
    public boolean esValido(int x, int y){
        char c1 =this.celdas[y][x].getElementoEnCelda(); 
        char c=this.celdas[y][x].getTipoTerreno();
        return ((c == ' ' || c == 'e' || c == 'L' || c == ',') && c1 == ' ');
    }
    
    public void cargaTilesEnMapa(ArrayList<BufferedImage> tiles){
        for(int i=0;i<this.n;i++){
            for (int j=0;j<this.m;j++){
                BufferedImage img =null;
                 if (this.celdas[i][j].getTipoTerreno()==',')img=tiles.get(6);
                 if (this.celdas[i][j].getTipoTerreno()=='M')img=tiles.get(5);
                 if (this.celdas[i][j].getTipoTerreno()=='L')img=tiles.get(4);
                if (this.celdas[i][j].getTipoTerreno()=='E')img=tiles.get(3);
                if (this.celdas[i][j].getTipoTerreno()=='e')img=tiles.get(2);
                if (this.celdas[i][j].getTipoTerreno()=='.')img=tiles.get(1);
                if (this.celdas[i][j].getTipoTerreno()==' ')img=tiles.get(0);
                this.celdas[i][j].setTile(img);
            }
        }
    }
    
}
